package edu.unl.bsm.CoolingLoad.Main;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import jxl.read.biff.BiffException;
import edu.unl.bsm.CoolingLoad.Dao.CoolingLoadDBDaoImpl;
import edu.unl.bsm.CoolingLoad.Dao.GREEDODaoImpl;
import edu.unl.bsm.CoolingLoad.Email.EmcsMailer;
import edu.unl.bsm.CoolingLoad.ErrorTesting.ErrorTest;
import edu.unl.bsm.CoolingLoad.Forecast.Forecasting;

public class Main {

	boolean reset = true;
	
	public void goForcasting() throws BiffException, IOException, ParseException{
		Forecasting forecasting = new Forecasting();
		forecasting.insertMissingData();
		if(reset){
			forecasting.resetCofficient();
		}else{
			forecasting.predictCoolingLoad();
		}
	}
	
	
	public void goErrorTest(){
		String outLierContent = ErrorTest.checkOutlier();
		List<String> managers = new ArrayList<String>();
		managers.add("gerfield@163.com");
		if(!outLierContent.equals("")){
			EmcsMailer.sendEmail("emcs@unl.edu",
					managers, "Cooling Load test", outLierContent, 
					"emcs-ws-app1.unl.edu", "");
		}
		
	}
	
	public void goSendingReport(){
		GREEDODaoImpl gdi = GREEDODaoImpl.getInstance();
		CoolingLoadDBDaoImpl ld = CoolingLoadDBDaoImpl.getInstance();
		List<Object[]> clValue = ld.getPredictedValue();
		Collections.reverse(clValue);
		
		List<Double> rmse = ld.getRobustMeanSquaredError();
		Collections.reverse(rmse);
		StringBuilder mailContent = new StringBuilder();
		
		String mailTitle = String.format("%15s    | %s | %s | %s |\n", "Target Hour", "Lower Confident Interval", "Cooling Load Value",  "Upper Confident Interval");
		mailContent.append(mailTitle);
//		System.out.println(mailContent);
		int index = 0;
		for(Object[] el: clValue){
			String report = new String();
			if(index < 7){
				report = String.format("%s| %15.2f          | %12.2f       | %15.2f          |\n", el[0].toString().substring(0,19), ((double)el[1]- 2 * rmse.get(index)) <=0 ? 0 :  (double)el[1]- 2 * rmse.get(index), (double)el[1],
						(double)el[1] + 2 * rmse.get(index));
			}else if(index >= 7 && index < 24){
				report = String.format("%s| %15.2f          | %12.2f       | %15.2f          |\n", el[0].toString().substring(0,19), ((double)el[1]- 2 * rmse.get(6)) <=0 ? 0 : (double)el[1]- 2 * rmse.get(6), (double)el[1],
						(double)el[1] + 2 * rmse.get(6));
			}else{
				report = String.format("%s| %15s          | %12.2f       | %15s          |\n", el[0].toString().substring(0,19),"N/A", (double)el[1], "N/A");
			}
			
			index++;
			mailContent.append(report);
		}
		
		System.out.println(mailContent);
		EmcsMailer.sendEmail("emcs@unl.edu",
				 gdi.getEmailRecipientsTest(), "Cooling Load test", mailContent.toString(), 
				"emcs-ws-app1.unl.edu", "");
	}
	/**
	 * @param args
	 * @throws ParseException 
	 * @throws IOException 
	 * @throws BiffException 
	 */
	public static void main(String[] args) throws BiffException, IOException, ParseException{
		
		Main start = new Main();
		start.goForcasting();
		start.goErrorTest();
		start.goSendingReport();

	}

}
